<?php
/**
 * application View
 * @author bbqq <toughgamer@gmail.com>
 * @package com.blib.app
 * @copyright bbqq qstufie.com
 */
class Blib_App_View extends Blib_Class_Atomic
{
    /**
     * the collection of vars
     * @var Blib_Data_Collection
     */
    protected $_vars;


    /**
     * constructor
     * @param str $ns namespace
     * @return void
     */
    public function __construct($ns)
    {
        // setup the params collection
        $this->_vars = new Blib_Data_Collection;
        // add default var
        // 1. template path
        $this->_setSys('path', realpath(Blib::getLoadPath($ns) . 'template') . BDS);
        // 2. the stack
        $this->_setSys('HTML', new Blib_View_Stack);
        // 3. auto render? yes
        $this->_setSys('auto_render', true);

    }// end __construct


    /**
     * auto render?
     * @param bool $isAutoRender if true, do auto render, otherwise skip
     * @return self
     */
    public function autoRender($isAutoRender)
    {
        $this->_setSys('auto_render', (bool) $isAutoRender);

    }// end $isAutoRender


    /**
     * set sys vals
     * @param str $key the key
     * @param mixed $val the value
     * @return self
     */
    protected function _setSys($key, $val)
    {
        return $this->set('_sys_' . $key, $val);

    }// end _setSys


    /**
     * get system vals
     * @param str $key the key
     * @param mixed $default the default value
     * @return mixed
     */
    protected function _getSys($key, $default = null)
    {
        return $this->get('_sys_' . $key, $default);

    }// end _getSys


    /**
     * set public key/val pair for rendering
     * @param str $key the key
     * @param mixed $val the value
     * @return self
     */
    public function set($key, $val)
    {
        $this->_vars->add($key, $val);
        return $this;

    }// end set


    /**
     * get the vars out
     * @param str $key the key
     * @param mixed $defaul if not found, the default
     * @return mixed
     */
    public function get($key, $default = null)
    {
        return $this->_vars->get($key, $default);

    }// end get


    /**
     * remove vars from the collection
     * @param str $key
     * @return self
     */
    public function remove($key)
    {
        // note, internal vars will not be removed
        if (strpos($key, '_sys_') !== false) {
            return $this;
        }
        $this->_vars->remove($key);
        return $this;

    }// end remove


    /**
     * render the view elements
     * to be implemented at the end
     * @return void
     */
    public function render()
    {
        if (!$this->_getSys('auto_render')) {
            return;
        }
        // start auto render
        // find out the method/controller name to get the final result
        $tmp = Blib_App_Delegate::getDefaultTemplate();
        if (empty($tmp)) {
            // unable to render, just stop here.
            return;
        }
        // otherwise, use the default dir
        $tmp = $this->_getSys('path') . $tmp;
        if (!file_exists($tmp)) {
            throw new exception('Template: ' . $tmp . ' does not exist');
        }
        include $tmp;

    }// end render

/*--------------------------[a bit of magic here]--------------------------*/

    /**
     * incase some one wants to use a direct $class->var
     * @param str $name the name
     * @return mixed
     */
    public function __get($name)
    {
        // check from sys first..
        $val = $this->_getSys($name);
        if (!empty($val)) {
            return $val;
        }

        // dont worry about params
        return $this->get($name);

    }// end __get

}// end Blib_App_View
?>